<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>如果此对象是该类的子类，则返回 TRUE</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.is-subclass-of" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">is_subclass_of</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">is_subclass_of</span> &mdash; <span class="dc-title">如果此对象是该类的子类，则返回 <strong><code>TRUE</code></strong></span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.is-subclass-of-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>is_subclass_of</strong></span>
    ( <span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$class_name</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   如果对象 <code class="parameter">object</code>
   所属类是类 <code class="parameter">class_name</code>
   的子类，则返回 <strong><code>TRUE</code></strong>，否则返回 <strong><code>FALSE</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    自 PHP 5.0.3 起也可以用一个字符串来指定 <code class="parameter">object</code>
    参数（类名）。
   </p>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-6149">
    <p><strong>Example #1 <span class="function"><strong>is_subclass_of()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;define&nbsp;a&nbsp;class<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$oink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'moo'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;define&nbsp;a&nbsp;child&nbsp;class<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">WidgetFactory_Child&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$oink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'oink'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;a&nbsp;new&nbsp;object<br /></span><span style="color: #0000BB">$WF&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WidgetFactory</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$WFC&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WidgetFactory_Child</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WFC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;\$WFC&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;\$WFC&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WF</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;\$WF&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;\$WF&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;usable&nbsp;only&nbsp;since&nbsp;PHP&nbsp;5.0.3<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #DD0000">'WidgetFactory_Child'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;WidgetFactory_Child&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;WidgetFactory_Child&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory
</pre></div>
    </div>
   </div>
  </p>
  <p class="simpara">
   参见 <span class="function"><a href="get_class.html" class="function">get_class()</a></span>、 
   <span class="function"><a href="get_parent_class.html" class="function">get_parent_class()</a></span> 和 
   <span class="function"><a href="is_a.html" class="function">is_a()</a></span>。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.is-subclass-of-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">object</code></dt>

     <dd>

      <p class="para">
       A class name or an object instance
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">class_name</code></dt>

     <dd>

      <p class="para">
       The class name
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">allow_string</code></dt>

     <dd>

      <p class="para">
       If this parameter set to false, string class name as <code class="parameter">object</code>
       is not allowed. This also prevents from calling autoloader if the class doesn&#039;t exist.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.is-subclass-of-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   This function returns <strong><code>TRUE</code></strong> if the object <code class="parameter">object</code>,
   belongs to a class which is a subclass of 
   <code class="parameter">class_name</code>, <strong><code>FALSE</code></strong> otherwise.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.is-subclass-of-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.9</td>
       <td>
        Added <code class="parameter">allow_string</code> parameter
       </td>
      </tr>

      <tr>
       <td>5.0.3</td>
       <td>
        You may also specify the <code class="parameter">object</code> parameter as a
        string (the name of the class)
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.is-subclass-of-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6150">
    <p><strong>Example #2 <span class="function"><strong>is_subclass_of()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;define&nbsp;a&nbsp;class<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$oink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'moo'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;define&nbsp;a&nbsp;child&nbsp;class<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">WidgetFactory_Child&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$oink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'oink'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;a&nbsp;new&nbsp;object<br /></span><span style="color: #0000BB">$WF&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WidgetFactory</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$WFC&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WidgetFactory_Child</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WFC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;</span><span style="color: #0000BB">$WFC</span><span style="color: #DD0000">&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;</span><span style="color: #0000BB">$WFC</span><span style="color: #DD0000">&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WF</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;</span><span style="color: #0000BB">$WF</span><span style="color: #DD0000">&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;</span><span style="color: #0000BB">$WF</span><span style="color: #DD0000">&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /><br /></span><span style="color: #FF8000">//&nbsp;usable&nbsp;only&nbsp;since&nbsp;PHP&nbsp;5.0.3<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #DD0000">'WidgetFactory_Child'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;WidgetFactory_Child&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;WidgetFactory_Child&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.is-subclass-of-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">如果此类不是已知类，使用此函数会使用任何已注册的
<a href="language.oop5.autoload.html" class="link">autoloader</a>。</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.is-subclass-of-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="get_class.html" class="function" rel="rdfs-seeAlso">get_class()</a> - 返回对象的类名</span></li>
    <li class="member"><span class="function"><a href="get_parent_class.html" class="function" rel="rdfs-seeAlso">get_parent_class()</a> - 返回对象或类的父类名</span></li>
    <li class="member"><span class="function"><a href="is_a.html" class="function" rel="rdfs-seeAlso">is_a()</a> - 如果对象属于该类或该类是此对象的父类则返回 TRUE</span></li>
    <li class="member"><span class="function"><a href="class_parents.html" class="function" rel="rdfs-seeAlso">class_parents()</a> - 返回指定类的父类。</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>